package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.entity.FsdChestChestGroup;
import com.scs.application.modules.fsd.mapper.ChestChestGroupMapper;
import com.scs.application.modules.fsd.service.ChestChestGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class ChestChestGroupServiceImpl extends BaseServiceImpl<ChestChestGroupMapper, FsdChestChestGroup> implements ChestChestGroupService {
    @Override
    public List<Map<String, Object>> getChestGroupsByChest(String id) {
        return baseMapper.getChestGroupsByChest(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeChestGroupsByChest(String chestGroupIds, String chestId) {
        remove(new QueryWrapper<FsdChestChestGroup>().eq("chest_id",chestId));
        Collection<FsdChestChestGroup> chestChestGroups = new ArrayList<>();
        String[] chestGroupIdArray = chestGroupIds.split(",");
        for (String chest_group_id:chestGroupIdArray) {
            if (StringUtils.isBlank(chest_group_id)) continue;
            FsdChestChestGroup chestChestGroup = new FsdChestChestGroup();
            chestChestGroup.setChestGroupId(chest_group_id);
            chestChestGroup.setChestId(chestId);
            chestChestGroups.add(chestChestGroup);
        }
        saveBatch(chestChestGroups);
        return true;
    }

    @Override
    public List<Map<String, Object>> getChestsAndStationsByChestGroup(String id) {
        return baseMapper.getChestsAndStationsByChestGroup(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeChestsByChestGroup(String chestIds, String chestGroupId) {
        remove(new QueryWrapper<FsdChestChestGroup>().eq("chest_group_id",chestGroupId));
        Collection<FsdChestChestGroup> chestChestGroups = new ArrayList<>();
        String[] chestIdArray = chestIds.split(",");
        for (String chest_id:chestIdArray) {
            if (StringUtils.isBlank(chest_id)) continue;
            FsdChestChestGroup chestChestGroup = new FsdChestChestGroup();
            chestChestGroup.setChestGroupId(chestGroupId);
            chestChestGroup.setChestId(chest_id);
            chestChestGroups.add(chestChestGroup);
        }
        saveBatch(chestChestGroups);
        return true;
    }
}
